package DFS;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ChanZany
 * @Date 2021/5/18 12:41
 * @Version 1.0
 */
public class permutations {
    //打印所有可能的输出结果
    private static void dfs(List<List<Integer>> res, List<Integer> cur, int[] arr, int[] visit) {
        if (cur.size() == arr.length) {
            res.add(new ArrayList<>(cur));
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            if (visit[i] == 1) continue;
            cur.add(arr[i]);
            visit[i] = 1;
            dfs(res, cur, arr, visit);
            cur.remove(cur.size() - 1);
            visit[i] = 0;
        }

    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        int[] visit = new int[arr.length];
//        dfs(arr,0,visit);
        List<List<Integer>> res = new ArrayList<>();
        dfs(res, new ArrayList<>(), arr, visit);
        res.forEach(System.out::println);
    }
}
